package com.taotao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.ContentMapper;
import com.taotao.pojo.Content;
import com.taotao.pojo.ContentCategory;
import com.taotao.pojo.Item;
import com.taotao.service.ContentService;
import com.taotao.util.ExceptionUtil;
import com.taotao.util.TaotaoResult;
import com.taotao.util.model.EasyUIResult;
import com.taotao.util.model.EasyUITreeModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * describe:内容Controller
 * @author hong.zheng
 * @date 2018/09/16
 */
@RequestMapping("/content")
@Controller
public class ContentController {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Autowired
    private ContentService contentService;

    @RequestMapping("/query/list")
    //设置相应的内容为json数据
    @ResponseBody
    public EasyUIResult getContentList(Long categoryId, Integer page, Integer rows){
        PageInfo<Content> pageInfo = contentService.getContentList(categoryId,page,rows);
        EasyUIResult<Content> result = new EasyUIResult<Content>(pageInfo.getTotal(), pageInfo.getList());
        return result;
    }
    @RequestMapping("/save")
    @ResponseBody
    public TaotaoResult addContent(Content content) throws Exception {
        try
        {
            contentService.insertContent(content);
            return TaotaoResult.ok();
        }
        catch(Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public TaotaoResult updateContent(Content content) throws Exception {
        try
        {
            contentService.updateContent(content);
            return TaotaoResult.ok();
        }
        catch(Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public TaotaoResult deleteContent(long []ids) throws Exception {
        try
        {
            contentService.deleteContent(ids);
            return TaotaoResult.ok();
        }
        catch(Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
    }
}
